package com.ks.core.util;

import org.apache.http.conn.ClientConnectionManager;
import org.apache.http.conn.scheme.Scheme;
import org.apache.http.conn.scheme.SchemeRegistry;
import org.apache.http.conn.ssl.SSLSocketFactory;
import org.apache.http.conn.ssl.X509HostnameVerifier;
import org.apache.http.impl.client.DefaultHttpClient;

import javax.net.ssl.*;
import java.io.IOException;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;

public class SSLClient extends DefaultHttpClient
{
  public SSLClient()
    throws Exception
  {
    SSLContext sslContext = SSLContext.getInstance("TLS");
    X509TrustManager tm = new X509TrustManager()
    {
      public void checkClientTrusted(X509Certificate[] x509Certificates, String s)
        throws CertificateException
      {
      }

      public void checkServerTrusted(X509Certificate[] x509Certificates, String s) throws CertificateException
      {
      }

      public X509Certificate[] getAcceptedIssuers()
      {
        return null;
      }
    };
    sslContext.init(null, new TrustManager[] { tm }, null);
    SSLSocketFactory ssf = new SSLSocketFactory(sslContext, new X509HostnameVerifier()
    {
      public boolean verify(String s, SSLSession sslSession) {
        return true;
      }

      public void verify(String host, SSLSocket ssl)
        throws IOException
      {
      }

      public void verify(String host, X509Certificate cert)
        throws SSLException
      {
      }

      public void verify(String host, String[] cns, String[] subjectAlts)
        throws SSLException
      {
      }
    });
    ClientConnectionManager ccm = getConnectionManager();
    SchemeRegistry sr = ccm.getSchemeRegistry();
    sr.register(new Scheme("https", 443, ssf));
  }
}